package org.tiger.netty.server.first;

import java.nio.Buffer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * @author lwl
 * @date 2020/9/17 10:31 下午
 */
@ChannelHandler.Sharable
public class TestServerHandler
		extends SimpleChannelInboundHandler<HttpObject> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
			throws Exception {
		// Buffer
		if (msg instanceof HttpRequest){
			ByteBuf content = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
			FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
					HttpResponseStatus.OK, content);
			fullHttpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
			fullHttpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
			ctx.writeAndFlush(fullHttpResponse);
		}

	}
}
